import { DialogForm } from 'custom/index'
import { inputAccountInputRecordData, dateNotCheck } from './api'
import HalfInput from './component/HalfInput.vue'
import { DateRangePicker } from '@/components/form-item'
import CustomField from './component/CustomField.vue'
import ZeroInput from './component/ZeroInput.vue'

const vConversionRate = (_: any, value: string, cb: any) => {
  console.log(value)
  if (/^\d+$/.test(value)) return cb()
  if (/^\d+:\d+$/.test(value)) return cb()
  cb(new Error('请输入正确格式，如2680:2020'))
}

const inputDataDialog = new DialogForm('DialogForm')

inputDataDialog
  .title('录入数据')
  .width('800px')
  .getApi(async (_, row) => ({ date: row.date, accountNo: row.accountNo, accountName: row.accountName, subAccountId: row.subAccountId }))
  .postApi((data => {
    let qaratio = data.qaratio
    if (/^\d+:\d+$/.test(qaratio)) {
      const [a, b] = qaratio.split(':')
      qaratio = 100 * a / b
    }
    return inputAccountInputRecordData({ ...data, qaratio: Math.round(qaratio) })
  }))
  .useGroup('')
    .useDatePicker('date').label('服务日期').setProp('disabled', true).end()
    .useItem('accountNo').label('数据账号').setProp('disabled', true).end()
    .useItem('accountName').label('子账号名称').setProp('disabled', true).end()
    .useCustomItem(HalfInput, 'salesRevenue').label('销售额(元)').required().end()
    .useCustomItem(HalfInput, 'receptionVolume').label('接待量(人)').required().end()
    .useItem('conversionRate').label('转化率(%)').required().end()
    .useItem('responseTime').label('响应时间(秒)').required().end()
    .useItem('qaratio').label('问答比(%)').setProp('placeholder', '请输入答问比，如2680:2020').validator(vConversionRate).required().end()
    .useCustomItem(ZeroInput, 'responseRate').label('回复率(%)').required().end()
    .useCustomItem(CustomField, 'customField').label('自定义字段').end()

const nottodayDialog = new DialogForm('DialogForm')

nottodayDialog
  .title('当天不检测')
  .getApi(async(_, row) => {
    return { subAccountId: row.subAccountId, date: row.date }
  })
  .postApi(dateNotCheck)
  .useGroup('')
    .useDateRangePicker(['startDate', 'endDate'], {
      tag: DateRangePicker,
      label: '日期范围'
    }).required().end()

export { inputDataDialog, nottodayDialog }
